<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="流程名称">
        <el-input v-model="workflowName" :readonly="true" />
      </el-form-item>

      <el-form-item label="项目名称">
        <el-input v-model="projectName" :readonly="true" />
      </el-form-item>

    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['workflow:meterage:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['workflow:meterage:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['workflow:node:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="nodeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="节点UUID" align="center" prop="workflowNodeUuid" />
      <el-table-column label="节点名称" align="center" prop="workflowNodeName" />
      <el-table-column label="节点顺序" align="center" prop="workflowNodeOrder" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['workflow:meterage:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['workflow:meterage:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-user-solid"
            @click="handleAddUser(scope.row)"
            v-hasPermi="['workflow:meterage:add']"
          >人员</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改流程管理-计量流程节点对话框 -->
    <el-dialog :title="title" :visible.sync="open" :close-on-click-modal="false" style="margin-top: 100px" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="节点名称" prop="workflowNodeName">
          <el-input v-model="form.workflowNodeName" placeholder="请输入节点名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 人员分配 -->
    <el-dialog :title="transferTitle" :visible.sync="transferOpen" :close-on-click-modal="false" width="850px" append-to-body>
      <el-row>
        <el-select v-model="transferProjectId" placeholder="建设项目" style="width: 280px;" @change="handleTransferProjectOnChange">
          <el-option
            v-for="item in searchProjectOptions"
            :key="item.projectId"
            :label="item.projectName"
            :value="item.projectId">
          </el-option>
        </el-select>
      </el-row>
      <el-transfer  style="padding-top: 10px;"
        :titles="['未分配', '已分配']"
        class="custom-transfer"
        filterable
        v-model="transferValue"
        :data="transferData"
        :props="{key: 'userName', label: 'nickName'}">
<!--        <template slot-scope="{ option }">-->
<!--          <el-tooltip class="item" v-if="option.nickName.length > 6" effect="dark" :content="option.nickName" placement="top" >-->
<!--            <span>{{ option.nickName }}</span>-->
<!--          </el-tooltip>-->
<!--          <span v-else>{{ option.nickName }}</span>-->
<!--        </template>-->
      </el-transfer>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSaveUser">确 定</el-button>
        <el-button @click="cancelAddUser">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listNode, getNode, delNode, addNode, updateNode, getWorkflowUserList, saveWorkflowNodeUser } from "@/api/workflow/workflowMeterageNode";
import { getMeterage } from "@/api/workflow/workflowMeterage";
import { getProjectByCode, selectProjectList} from "@/api/project/project";

export default {
  name: "Node",
  data() {
    return {
      // 遮罩层
      loading: false,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 流程管理-计量流程节点表格数据
      nodeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 项目编号
      projectCode: undefined,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        workflowUuid: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        workflowNodeName: [
          { required: true, message: "节点名称不能为空", trigger: "blur" },
          { max: 64, message: '节点名称最大长度为50个字符', trigger: 'blur'}
        ]
      },
      // 项目名称
      projectName: null,
      // 流程名称
      workflowName: null,
      // 分配用户标题
      transferTitle: "",
      transferProjectId: undefined,
      // 查询条件 建设项目选项
      searchProjectOptions: [],
      // 是否显示弹出层
      transferOpen: false,
      // 未分配用户
      transferData: [],
      // 已分配用户
      transferValue: [],
      // 流程节点UUID
      workflowNodeUuid: null
    };
  },
  created() {
    this.queryParams.workflowUuid = this.$route.query.workflowUuid;
    this.projectCode = this.$route.query.projectCode;
    this.getMeterageModel();
    this.getList();
    this.getProject();
  },
  methods: {
    getMeterageModel() {
      getMeterage(this.queryParams.workflowUuid).then(response => {
        this.projectName = response.data.projectName;
        this.workflowName = response.data.workflowName;
      });
    },
    // 加载建设项目
    getProject() {
      getProjectByCode({projectCode: this.projectCode}).then(response => {
        this.transferProjectId = response.data.projectId;
        this.getProjectList();
      })
    },
    /** 建设项目下拉 */
    getProjectList() {
      selectProjectList().then(response => {
        this.searchProjectOptions = response.projects;
      })
    },
    /** 查询流程管理-计量流程节点列表 */
    getList() {
      this.loading = true;
      listNode(this.queryParams).then(response => {
        this.nodeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        workflowNodeUuid: null,
        workflowUuid: null,
        workflowNodeName: null,
        workflowNodeOrder: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.workflowNodeUuid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加计量流程节点";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const workflowNodeUuid = row.workflowNodeUuid || this.ids
      getNode(workflowNodeUuid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改计量流程节点";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.workflowUuid = this.queryParams.workflowUuid;
          if (this.form.workflowNodeUuid != null) {
            updateNode(this.form).then(response => {
              this.$modal.msgSuccess("操作成功");
              this.open = false;
              this.getList();
            });
          } else {
            addNode(this.form).then(response => {
              this.$modal.msgSuccess("操作成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const workflowNodeUuids = row.workflowNodeUuid || this.ids;
      this.$modal.confirm('是否确认删除选中的数据项？').then(function() {
        return delNode(workflowNodeUuids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {});
    },
    /** 分配人员 */
    handleAddUser(row) {
      console.log("row：" + JSON.stringify(row));
      this.workflowNodeUuid = null;
      this.transferData = [];
      this.transferValue = [];
      this.transferAllData = [];
      this.workflowNodeUuid = row.workflowNodeUuid;
      getWorkflowUserList({ workflowNodeUuid: this.workflowNodeUuid }).then(response => {
        // this.transferData = response.users;
        // this.transferValue = response.checkedUsers;
        this.transferAllData = response.users;
        this.transferValue = response.checkedUsers;
        this.transferUser();
        this.transferOpen = true;
        this.transferTitle = "计量流程-分配用户";
      })
    },
    // 项目发生改变，穿梭器数据绑定
    handleTransferProjectOnChange() {
      this.transferUser();
    },
    transferUser() {
      let leftUser = [];
      this.transferAllData.forEach(item => {
        let flag = false;
        if (item.projectId == this.transferProjectId) {
          leftUser.push(item);
          flag = true;
        }
        if (this.transferValue) {
          this.transferValue.forEach(code => {
            if (item.userName == code && !flag) {
              leftUser.push(item);
            }
          });
        }
      });
      this.transferData = leftUser;
    },
    /** 分配人员保存 */
    handleSaveUser() {
      let users = '';
      this.transferValue.forEach((item) => {
        users += item + ',';
      })
      users = users.substring(0, users.length - 1);
      saveWorkflowNodeUser({ workflowNodeUuid: this.workflowNodeUuid, users: users}).then(response => {
        this.$modal.msgSuccess("分配成功");
        // this.workflowNodeUuid = null;
        // this.transferData = [];
        // this.transferValue = [];
        this.transferOpen = false;
      })
    },
    /** 取消分配人员 */
    cancelAddUser() {
      // this.workflowNodeUuid = null;
      // this.transferData = [];
      // this.transferValue = [];
      this.transferOpen = false;
    },
  }
};
</script>

<style lang="scss">

  .custom-transfer .el-transfer-panel {
    width: 280px; /* 设置宽度为300px */
  }
</style>
